package com.example.recyclerviewdemo.demo0

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class Demo0ViewModel: ViewModel() {

    //当前获取到的数据
    val rvList = MutableLiveData<MutableList<String>?>()

    /*
    * 获取空列表
    * */
    fun getEmptyList(){
        rvList.value = mutableListOf()
    }

    /*
    * 获取错误列表
    * */
    fun getErrorList(){
        rvList.value = null
    }

    /*
    * 获取正常列表
    * count 需要的数据长度
    * */
    fun getNormalList(count: Int){
        if(count == -1){
            getErrorList()
            return
        }
        val list = mutableListOf<String>()
        //模拟从网络获取数据
        viewModelScope.launch (Dispatchers.IO){
            for(i in 0 until count){
                list.add("item数据")
            }
            delay(2000)
            rvList.postValue(list)
        }
    }

}